package com.miyake.dsp.osa.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;

import com.miyake.dsp.ui.LineChartPanel;

public abstract class OsaChartUI extends JFrame {
	public abstract void onWdmSource();
	public abstract void onAnalysis();
	public abstract void onResultTable();
	public abstract void onSaveXML(String path);
	public abstract void onSaveSetting(String path);
	public abstract void onRecallSetting(String path);
	public abstract void onActualResolution();
	public abstract void onSaveText(String path);
	public abstract void onSaveTable(String path);
	public abstract void onSaveYokogawa(String path);
	
	private LineChartPanel chart;
	private JFileChooser filechooser;
	private JFileChooser filechooserSetting;
	private JFileChooser filechooserText;
	private JFileChooser filechooserTable;
	
	public OsaChartUI() throws HeadlessException {
		super();
		// TODO Auto-generated constructor stub
		initialize();
	}

	private void initialize() {
		this.setTitle("MS9740A Opt Amp (Multi Channel) Test Tool BETA");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(new Dimension(700, 400));
		chart = new LineChartPanel("WDM Source Trace");
		
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(createToolBar(), BorderLayout.NORTH);
		this.getContentPane().add(chart, BorderLayout.CENTER);
	}

	private Component createToolBar() {
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout());
		JButton  wdmButton = new JButton("Source...");
		p.add(wdmButton);
		wdmButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onWdmSource();
			}
		});
		
		JButton anaButton  = new JButton("Analysis");
		p.add(anaButton);
		anaButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onAnalysis();
			}
		});
		
		JButton resultButton  = new JButton("Result");
		p.add(resultButton);
		resultButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				onResultTable();
			}
		});		
		
		JButton xmlButton  = new JButton("Save XML");
		p.add(xmlButton);
		xmlButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showSaveDialog();
			}
		});	
		
		JButton settingButton  = new JButton("Save Setting");
		p.add(settingButton);
		settingButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showSaveSettingDialog();
			}
		});	
		
		JButton recallButton  = new JButton("Recall");
		p.add(recallButton);
		recallButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showRecallSettingDialog();
			}
		});	
		
		JButton actresButton = new JButton("Res");
		p.add(actresButton);
		actresButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				onActualResolution();
			}
			
		});
		
		JButton exportButton = new JButton("Export");
		p.add(exportButton);
		exportButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				showSaveTextDialog();
			}
			
		});
		
		JButton tableButton = new JButton("Table");
		p.add(tableButton);
		tableButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveTable();
			}
			
		});
		
		JButton yokogawaButton = new JButton("Yokogawa");
		p.add(yokogawaButton);
		yokogawaButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				saveYokogawa();
			}
			
		});
		return p;
	}

	protected void saveYokogawa() {
		if (filechooserTable == null) {
			filechooserTable = createTableFileChooser();
		}

		int selected = filechooserTable.showSaveDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooserTable.getSelectedFile();
		  String fname = file.getAbsolutePath();
//		  if (!fname.endsWith(".csv") && !fname.endsWith(".CSV")) {
//			  fname += ".csv";
//		  }
		  onSaveYokogawa(fname);
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}
	}
	protected void saveTable() {
		if (filechooserTable == null) {
			filechooserTable = createTableFileChooser();
		}

		int selected = filechooserTable.showSaveDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooserTable.getSelectedFile();
		  String fname = file.getAbsolutePath();
		  if (!fname.endsWith(".csv") && !fname.endsWith(".CSV")) {
			  fname += ".csv";
		  }
		  onSaveTable(fname);
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}

	}
	
	private JFileChooser createTableFileChooser() {
		JFileChooser chooser = new JFileChooser();
		chooser.addChoosableFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				return f.getName().endsWith("csv");
			}

			@Override
			public String getDescription() {
				return "Table";
			}
			
		});
		
		return chooser;
	}
	
	protected void showSaveTextDialog() {
		if (filechooserText == null) {
			filechooserText = createTextFileChooser();
		}

		int selected = filechooserText.showSaveDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooserText.getSelectedFile();
		  onSaveText(file.getAbsolutePath());
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}
	}
	private JFileChooser createTextFileChooser() {
		JFileChooser chooser = new JFileChooser();
		chooser.addChoosableFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				return f.getName().endsWith("text");
			}

			@Override
			public String getDescription() {
				return "TEXT";
			}
			
		});
		
		return chooser;
	}
	protected void showRecallSettingDialog() {
		if (filechooserSetting == null) {
			filechooserSetting = createSettingFileChooser();
		}

		int selected = filechooserSetting.showOpenDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooserSetting.getSelectedFile();
		  onRecallSetting(file.getAbsolutePath());
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}
	}
	public LineChartPanel getChart() {
		return chart;
	}

	private void showSaveSettingDialog() {
		if (filechooserSetting == null) {
			filechooserSetting = createSettingFileChooser();
		}

		int selected = filechooserSetting.showSaveDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooserSetting.getSelectedFile();
		  onSaveSetting(file.getAbsolutePath());
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}
	}
	private JFileChooser createSettingFileChooser() {
		JFileChooser chooser = new JFileChooser();
		chooser.addChoosableFileFilter(new FileFilter() {
			@Override
			public boolean accept(File f) {
				return f.getName().endsWith("tst");
			}

			@Override
			public String getDescription() {
				return "Setting";
			}
			
		});
		
		return chooser;
	}
	
	private void showSaveDialog() {
		if (filechooser == null) {
			filechooser = new JFileChooser();
			filechooser.addChoosableFileFilter(new FileFilter() {
				@Override
				public boolean accept(File f) {
					return f.getName().endsWith("xml");
				}

				@Override
				public String getDescription() {
					return "MS9740A XML";
				}
				
			});
		}

		int selected = filechooser.showSaveDialog(this);
		if (selected == JFileChooser.APPROVE_OPTION){
		  File file = filechooser.getSelectedFile();
		  onSaveXML(file.getAbsolutePath());
		  
		}else if (selected == JFileChooser.CANCEL_OPTION){
		}else if (selected == JFileChooser.ERROR_OPTION){
		}
	}
	
}
